
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>自定义配置</title>
    {include file="include/top"}
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <button class="layui-btn layuiadmin-btn-tags" data-type="add">添加配置信息</button>
        </div>
        <div class="layui-card-body">
            <div id="test12" class="demo-tree-more"></div>
            <div  id="authlist" class="demo-tree-more"></div>
        </div>
    </div>
</div>
{include file="include/js"}

<script>
    var pathid=0;
    var gid= {$gid};
    layui.config({
        base: '../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'contlist', 'table'], function(){
        var table = layui.table;
        var $ = layui.$, active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加'
                        ,content: 'addclasslist?ty=1&pathid='+pathid
                    ,area: ['450px', '600px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags")
                            ,tags = othis.find('input[name="tags"]').val()
                            ,pathid=othis.find('input[name="pathid"]').val();
                        if(!tags.replace(/\s/g, '')) return;
                        $.post("editclasspost",{tags:tags,pathid:pathid},function (re) {
                            layer.msg(re.msg);
                            setTimeout(function () {
                                location.reload();
                            },2000);
                        },'json');
                    }
                });
            }
        }
        $('.layui-btn.layuiadmin-btn-tags').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });


    layui.use(['tree', 'util'], function() {
        var tree = layui.tree
            , layer = layui.layer
            , util = layui.util

            //模拟数据
            , data = [
                {
                    title: '一级1'
                    , id: 1
                    , field: 'name1'
                    , checked: true
                    , spread: true
                    , children: [{
                        title: '二级1-1 可允许跳转'
                        , id: 3
                        , field: 'name11'
                    }, {
                        title: '二级1-2'
                        , id: 4
                        , spread: true
                    }, {
                        title: '二级1-3'
                        , id: 20
                        , field: ''
                    }]
                },
                {
                    title: '一级1'
                    , id: 2
                    , field: 'name1'
                    , checked: true
                    , spread: true
                    , children: [{
                        title: '二级1-1 可允许跳转'
                        , id: 23
                        , field: 'name11'
                    }, {
                        title: '二级1-2'
                        , id: 24
                        , spread: true
                    }, {
                        title: '二级1-3'
                        , id: 220
                        , field: ''
                    }]
                }

            ];

        //基本演示
        var $ = layui.$;
        $.getJSON("classlistdata?gid="+gid,function (re) {
            // data=JSON.stringify(re);
            tree.render({
                elem: '#authlist'
                , data: re.data
                , showCheckbox: true  //是否显示复选框
                , id: 'demoId1'
                , isJump: true //是否允许点击节点时弹出新窗口跳转
                , click: function (obj) {
                    var data = obj.data;  //获取当前点击的节点数据
                    // layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
                    pathid=data.id;
                    var $conf=layer.confirm('选择操作类型？', {
                        btn: ['添加子分类','编辑','取消'] //按钮
                    }, function(){
                        layer.close($conf);
                        layer.msg('不能添加二级分类');
                        // $('.layuiadmin-btn-tags').click();
                    }, function(){
                        editdd();
                    });
                    // $('.layuiadmin-btn-tags').click();
                }
            });
        })
        function editdd() {
            layer.open({
                type: 2
                ,title: '修改分类'
                ,content: 'addclasslist?ty=1&gid='+pathid+'&pathid='+pathid
                ,area: ['450px', '400px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags")
                        ,tags = othis.find('input[name="tags"]').val();
                    if(!tags.replace(/\s/g, '')) return;
                    $.post("editclasspost",{tags:tags,gid:pathid},function (re) {
                        layer.msg(re.msg);
                        setTimeout(function () {
                            location.reload();
                        },2000);
                    },'json');
                }
            });
        }
        //按钮事件
        util.event('lay-demo', {
            getChecked: function(othis){
                var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据
                // layer.alert(JSON.stringify(checkedData), {shade:0});
                // console.log(checkedData);
                $.post("authgroupsave",{str:JSON.stringify(checkedData),gid:gid},function(re){
                    if(re.code==0){
                        layer.msg("修改成功");
                        setTimeout(function () {
                            history.go(-1);
                        },2000)
                    }
                },"json");
            }
            ,setChecked: function(){
                tree.setChecked('demoId1', [3, 4]); //勾选指定节点
            }
            ,reload: function(){
                //重载实例
                tree.reload('demoId1', {

                });

            }
        });
    });
</script>
</body>
</html>
